// chat-widget.js
document.addEventListener('DOMContentLoaded', function() {
    // 创建客服按钮并添加样式
    var chatButton = document.createElement('div');
    chatButton.id = 'customer-service-btn';
    // chatButton.textContent = '客服';
    chatButton.style.position = 'fixed';
    chatButton.style.bottom = '20px';
    chatButton.style.right = '20px';
    // chatButton.style.backgroundColor = '#4CAF50';
    // chatButton.style.color = '#fff';
    chatButton.style.padding = '10px';
    chatButton.style.cursor = 'pointer';
    //chatButton.style.borderRadius = '5px';
    chatButton.style.backgroundImage = "url('https://ajmall-images.oss-us-east-1.aliyuncs.com/app-image/icon/aivochat.png')";
    chatButton.style.backgroundSize = "cover";
    chatButton.style.backgroundRepeat = "no-repeat"
    chatButton.style.width = "40px";
    chatButton.style.height = "40px";
    chatButton.style.zIndex = '999';
    chatButton.onclick = toggleChat;
    
    // 创建客服弹窗并添加样式
    var chatPopup = document.createElement('div');
    chatPopup.id = 'customer-service-popup';
    chatPopup.style.display = 'none';
    chatPopup.style.position = 'fixed';
    chatPopup.style.bottom = '70px';
    chatPopup.style.right = '20px';
    chatPopup.style.backgroundColor = '#fff';
    // chatPopup.style.padding = '10px';
    chatPopup.style.width = '40%';
    chatPopup.style.height = '70%';
    chatPopup.style.zIndex = '998';
    chatPopup.style.borderRadius = '10px';

    // 创建 iframe 元素并设置 src 属性
    var iframe = document.createElement('iframe');
    iframe.id = 'aivoframe'
    iframe.style.width = '100%';
    iframe.style.border = 'none';
    iframe.style.borderRadius = '10px';
    iframe.style.boxShadow = 'rgba(0, 0, 0, 0.4) 0px 0px 10px';
    iframe.style.height = '100%'; // 设置 iframe 高度
    iframe.src = 'https://www.aivodesk.com/5/test01'; // 替换为你希望打开的网站地址
    
    // 将 iframe 添加到弹窗中
    chatPopup.appendChild(iframe);
    
    // 将客服按钮和弹窗添加到页面中
    document.body.appendChild(chatButton);
    document.body.appendChild(chatPopup);
    
    // 定义toggleChat函数
    function toggleChat() {
        //每次打开检查cookie
        var iframe = document.getElementById('aivoframe');
        var aivoid = localStorage.getItem('aivoid');
        if(aivoid && chatPopup.style.display === 'none') {
            var newurl = 'https://www.aivodesk.com/5/'.concat(aivoid);
            var oldurl = iframe.src;
            if(newurl!=oldurl){
                iframe.src = newurl;
            }
        }
        chatPopup.style.display = (chatPopup.style.display === 'none' || chatPopup.style.display === '') ? 'block' : 'none';
    }
    });
    